package com.nmdl.test2;

import android.app.Dialog;
import android.bluetooth.BluetoothDevice;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class DeviceActionFragment extends DialogFragment implements View.OnClickListener {
    private boolean mIsShowAnimation = true;
    private BluetoothDevice bluetoothDevice;
    private OnActionClick onActionClick;
    TextView device_name_tv;
    TextView mac_id_tv;

    public static DeviceActionFragment newInstance(BluetoothDevice device, OnActionClick onActionClick) {
        DeviceActionFragment fragment = new DeviceActionFragment(onActionClick);
        Bundle bundle = new Bundle();
        bundle.putParcelable("device", device);
        fragment.setArguments(bundle);
        return fragment;
    }

    public DeviceActionFragment(OnActionClick onActionClick) {
        this.onActionClick = onActionClick;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mRootView = inflater.inflate(getLayoutId(), container, false);
        initDialog();
        initView(mRootView);
        initEventAndData();
        return mRootView;
    }

    private int getLayoutId() {
        return R.layout.fragment_device_action;
    }

    private void initView(View mRootView) {
        Button remove_bond_btn = mRootView.findViewById(R.id.remove_bond_btn);
        Button send_message_btn = mRootView.findViewById(R.id.send_message_btn);
        device_name_tv = mRootView.findViewById(R.id.device_name_tv);
        mac_id_tv = mRootView.findViewById(R.id.mac_id_tv);
        remove_bond_btn.setOnClickListener(this);
        send_message_btn.setOnClickListener(this);
    }

    private void initEventAndData() {
        Bundle bundle = getArguments();
        bluetoothDevice = bundle.getParcelable("device");
        device_name_tv.setText("设备: " + bluetoothDevice.getName());
        mac_id_tv.setText("MAC: " + bluetoothDevice.getAddress());
    }

    public interface OnActionClick {
        void sendMessage();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.remove_bond_btn:
                Boolean remove = BleHelper.removeBonds(bluetoothDevice);
                Toast.makeText(this.getActivity(), remove ? "断开成功！" : "断开失败！", Toast.LENGTH_SHORT).show();
                this.dismiss();
                break;
            case R.id.send_message_btn:
                if (onActionClick != null)
                    onActionClick.sendMessage();
                break;
        }
    }

    private void initDialog() {
        //设置dialog样式
        Dialog dialog = getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置window样式
        Window window = dialog.getWindow();
        if (window != null) {
            if (mIsShowAnimation)
                window.getAttributes().windowAnimations = R.style.DatePickerDialogAnim;
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.gravity = Gravity.CENTER; // 紧贴底部
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT; // 宽度持平
            lp.dimAmount = 0.45f;
            window.setAttributes(lp);
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            //设置背景透明
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
    }
}
